package mx.com.daycare.managed;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import org.primefaces.event.SelectEvent;
import org.primefaces.event.UnselectEvent;

import mx.com.daycare.business.FamilyBLLocal;
import mx.com.daycare.entity.Family;
import mx.com.daycare.entity.Kid;
import mx.com.daycare.view.FamilyDataModel;

@ManagedBean
@ViewScoped
public class FamilyMB {

	private Family family;
	private Kid kid;	
	private FamilyDataModel familiesDataModel;
	private List<Family> familiesFiltered;	

	@EJB
	private FamilyBLLocal familyBL;

	public Family getFamily() {
		return family;
	}

	public void setFamily(Family family) {
		this.family = family;
	}

	public Kid getKid() {
		return kid;
	}

	public void setKid(Kid kid) {
		this.kid = kid;
	}

	public FamilyDataModel getFamiliesDataModel() {
		return familiesDataModel;
	}

	public void setFamiliesDataModel(FamilyDataModel familiesDataModel) {
		this.familiesDataModel = familiesDataModel;
	}

	public List<Family> getFamiliesFiltered() {
		return familiesFiltered;
	}

	public void setFamiliesFiltered(List<Family> familiesFiltered) {
		this.familiesFiltered = familiesFiltered;
	}	

	@PostConstruct
	public void searchFamilies() {
		familiesDataModel = new FamilyDataModel(familyBL.getFamilies());
	}

	public Date getToday(){
		return new Date();
	}
	
	public String initDialog() {
		family = new Family();
		kid = new Kid();		
		return null;
	}

	public String reinit() {
		kid = new Kid();
		System.out.println("REINIT");
		return null;
	}

	public String save() {
		System.out.println("GUARDAR FAMILIA");
		return null;
	}
	
	public String delete(){
		System.out.println("DELETE FAMILY " + family.getFirstName() + " " + family.getLastName());
		List<Family> temp = (List<Family>)familiesDataModel.getWrappedData();
		temp.remove(family);
		familiesDataModel = new FamilyDataModel(temp);
		return null;
	}

	public void onRowSelect(SelectEvent event) {

	}

	public void onRowUnselect(UnselectEvent event) {

	}
}
